<?php
class TemplatesController extends AppController {

	var $name = 'Templates';
	var $helpers = array('Html', 'Form');

    function index_admin() 
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            if(!empty($this->data))
            { 
                if($this->data['Template']['name'] == '' || $this->data['Template']['path'] == '')
                {
                    $this->set('empty',true);
                }
                else                
                if(!empty($this->data['Template']['id']))
                {
                    $count = $this->Template->find('count',array('conditions'=>array('Template.name'=>$this->data['Template']['name'],'Template.id <>'=>$this->data['Template']['id'])));
                    if( $count == 0 ) 
                    { $this->Template->save($this->data); }
                    else
                    { $this->set('wrong',true); } 
                }
                else
                {
                    $count = $this->Template->find('count',array('conditions'=>array('Template.name'=>$this->data['Template']['name'])));
                    if( $count == 0 ) 
                    { $this->Template->create(); $this->Template->save($this->data); }                
                    else
                    { $this->set('wrong',true); }
                }
            }
            $this->Template->recursive=-1;
            $templates = $this->Template->find('all',array('order'=>'Template.id'));
            $this->set('templates',$templates);
        }     
    }
                    
    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Template->del($id);
            $this->redirect(array('controller'=>'templates','action'=>'index_admin'));
        }        
    }    
    
	
}
?>